
import 'dart:ui';

import 'package:flutter/material.dart';

class ColorsUtil  {

  static int getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  //根据偏差值 ${positionOffset} 计算出两个颜色的过渡色
  static int getChangeColor(double positionOffset,int startColor,int endColor) {
    int redCurrent;
    int blueCurrent;
    int greenCurrent;
    int alphaCurrent;

    var startCl = Color(startColor);
    var endCl = Color(endColor);

    // Color.lerp(a, b, t)
    int redStart = startCl.red;
    int greenStart = startCl.green;
    int blueStart = startCl.blue;
    int alphaStart = startCl.alpha;

    int redEnd = endCl.red;
    int greenEnd = endCl.green;
    int blueEnd = endCl.blue;
    int alphaEnd = endCl.alpha;

    int redDifference = redEnd - redStart;
    int blueDifference = blueEnd - blueStart;
    int greenDifference = greenEnd - greenStart;
    int alphaDifference = alphaEnd - alphaStart;

    redCurrent = (redStart + positionOffset * redDifference).toInt();
    blueCurrent = (blueStart + positionOffset * blueDifference).toInt();
    greenCurrent = (greenStart + positionOffset * greenDifference).toInt();
    alphaCurrent =  (alphaStart + positionOffset * alphaDifference).toInt();
    Color argb = Color.fromARGB(alphaCurrent, redCurrent, greenCurrent, blueCurrent);
    return argb.value;
  }


}

